(ns try-proxy.core
  (:gen-class)
  (:use jsoup.soup)
  (:require [clj-http.client :as client]))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!"))


(defn get-proxy-list []
  (let [elems ($ (get! "http://www.xicidaili.com" :user-agent "CoCo/1.0")
                 "#ip_list"
                 "tr:has(td)")]
    (mapv
      #(let [[_ ip port] (-> % .children text)]
        {:ip ip :port (Integer/parseInt port)})
      elems)))

(defn fetch [{:keys [ip port]}]
  ;(print ip "   " port "     ")
  (try
    (let [start# (. System (nanoTime))]
      (client/get "http://www.baidu.com"
                  {:proxy-host     ip
                   :proxy-port     port
                   :socket-timeout 1000
                   :conn-timeout   1000})
      (println (str ip ":" port "\t" (/ (double (- (. System (nanoTime)) start#)) 1000000))))
    true
    (catch Exception e
      (println (str ip ":" port "\t" (.getMessage e)))
      ;(println (.getMessage e))
      false)))


(let [proxies (get-proxy-list)]
  (fetch {:ip "127.0.0.1" :port 8080})
  (doseq [proxy proxies]
    (fetch proxy)))
